/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 30318
 * Date: 2024-01-17
 * Time: 12:59
 */
import java.util.Scanner;

public class TestOfDesk {
    private Desk[] desks;

    public TestOfDesk() {
        desks = new Desk[20];//20个桌位
        for(int i = 0;i < desks.length; i++) {
            desks[i] = new Desk();
            int num = (int)(Math.random() * 3);
            desks[i].setDesknum(i + 1);
            setDesks(num, i);
        }
    }

    public void setDesks(int n, int desknum) {
        if(n == 0){
            desks[desknum].setState("空");
            desks[desknum].setBooked(false);
        }
        else if(n == 1){
            desks[desknum].setState("已预定");
            desks[desknum].setBooked(true);
        }
        else if(n == 2){
            desks[desknum].setState("已坐人");
            desks[desknum].setBooked(false);
        }
    }

    public void displayAllDesk() {
        System.out.println("**********   所有桌位   *********");
        System.out.println("桌位号\t\t桌位状态");
        for(int i = 0;i < desks.length;i++) {
            System.out.println(desks[i].toString());
        }
        System.out.println("*******************************");
    }
    public void bookDesk() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入要预订的桌子号数(1-20)：");
        int desknum = input.nextInt();
        if (desknum < 1 || desknum > 20) {
            System.out.println("输入桌子号码错误，请重新输入");
            return;
        }
        Desk desk = desks[desknum - 1];
        if (desk.getState().equals("已坐人")) {
            System.out.println("该桌位已经有人坐，请选择其他桌位");
            return;
        }
        if (desk.isBooked()) {
            System.out.println("该桌位已经被预订，请选择其他桌位");
            return;
        }
        desk.setState("已预定");
        desk.setBooked(true);
        System.out.println("成功预订桌位" + desknum);
    }
    public void cancelBooking() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入要退订的桌子号数(1-20)：");
        int desknum = input.nextInt();
        if (desknum < 1 || desknum > 20) {
            System.out.println("输入桌子号码错误，请重新输入");
            return;
        }
        Desk desk = desks[desknum - 1];
        if (!desk.isBooked()) {
            System.out.println("该桌位未被预订");
            return;
        }
        desk.setState("空");
        desk.setBooked(false);
        System.out.println("成功退订桌位" + desknum);
    }
}

class Desk {
    private int desknum;//桌子号码
    private String state;//桌子状态
    private boolean booked;

    public Desk() {
    }

    public int getDesknum() {
        return desknum;
    }

    public void setDesknum(int desknum) {
        this.desknum = desknum;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public boolean isBooked() { return booked; }

    public void setBooked(boolean booked) { this.booked = booked; }

    public String toString() {
        return getDesknum() + "\t\t" + getState();
    }
}